<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="ua.lk.utils.*" %>    
<!DOCTYPE html>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Пошук - Інформаційна служба 109</title>
	<link rel="shortcut icon" href="img/109ico4.ico" type="image/ico" />
	<link rel="stylesheet" href="css/styles.css" type="text/css" />
	<script type="text/javascript">
	

 		document.onkeydown = function(e) {
 			e = e || event;
 			if( e.keyCode == 27) {
 				clearForm(); 			 				
 			}
 		}
 		
 		

    	function clearForm(){
        	document.getElementById('search_submit').focus();
        	document.getElementById('fio').value = '';
        	document.getElementById('city').value = '';
        	document.getElementById('street').value = '';
        	document.getElementById('building').value = '';
        	document.getElementById('app').value = '';
        	document.getElementById('phone').value = '';
        	document.getElementById('fio').focus();
    	}
    	function orderby(s) {
    		document.getElementById('orderby').value = s;
    		document.getElementById('searchForm').submit();
    	}

    	// вызывается при полной загрузке страницы - 
    	function smartFocus()
    	{
    		if( document.getElementById('result').rows.length > 1 )
    			{document.getElementById('result').focus();}
    		else
    			{document.getElementById('fio').focus();};
    		
    	}
	</script>
	<style>a.m-search {font-weight:bold;} </style>
</head>

<% 
request.setCharacterEncoding("UTF-8"); 
String 	fioSearch = StringUtils.ifNull(request.getParameter("fio_search")),
		citySearch = StringUtils.ifNull(request.getParameter("city_search")),
		streetSearch = StringUtils.ifNull(request.getParameter("street_search")),
		buildingSearch = StringUtils.ifNull(request.getParameter("building_search")),
		appSearch = StringUtils.ifNull(request.getParameter("app_search")),
		phoneSearch = StringUtils.ifNull(request.getParameter("phone_search")),
		
		orderBy = StringUtils.ifNull(request.getParameter("orderby"),"OwnerName");
boolean conditionIsEmpty = true;
 %>

<sql:query var="rs" dataSource="jdbc/info09_2014">
select OwnerName, CityName, StreetName, Building, App, PhoneNumber   
from phones 
where 1 = 1
<%if( !fioSearch.equals("")) { conditionIsEmpty = false; %>
	and OwnerName like '<%=fioSearch %>%'
<% }
if( !citySearch.equals("")) { conditionIsEmpty = false; %>
   	and CityName like '%<%=citySearch%>%'
<% }   
if( !streetSearch.equals("")) { conditionIsEmpty = false; %>
   	and StreetName like '%<%=streetSearch%>%'
<% } 
if( !buildingSearch.equals("")) { conditionIsEmpty = false; %>
   	and Building like '<%=buildingSearch%>'
<% } 
if( !appSearch.equals("")) { conditionIsEmpty = false; %>
   	and App like '<%=appSearch%>'
<% } 
if( !phoneSearch.equals("")) { conditionIsEmpty = false; %>
   	and PhoneNumber like '<%=phoneSearch%>'
<% }
if( conditionIsEmpty ) {
%>   
	and 1 = 2
<% } %>	
order by <%=orderBy%>
limit 1000
<%-- <sql:param value = "%${param.query}%"/> --%>
<%-- <sql:param value = '<%="%"+fioSearch+"%"%>'/> --%>
</sql:query>

<body onload="smartFocus()">
<div class="menu-search">
<jsp:include page="includes/header.jsp"></jsp:include>
</div>
<h3>Пошук у довіднику</h3>
<div class="search-parameters">
<form id= "searchForm" action="mainsearch.jsp" method="post"  >
	<input type="text" name="fio_search" id="fio" value = '<%=fioSearch%>' placeholder="Прізвище" title="Якщо почати з символу '%', пошук буде здійснено у будь якому місці рядку, а не тільки з початку!"/>
	<input type="text" name="city_search" id="city" value = '<%=citySearch%>' placeholder="Місто"  />
	<input type="text" name="street_search" id="street" value = '<%=streetSearch%>' placeholder="Вулиця"  />
	<input type="text" name="building_search" id="building" value = '<%=buildingSearch%>' placeholder="Будинок"  />
	<input type="text" name="app_search" id="app" value = '<%=appSearch%>' placeholder="Квартира"  />
	<input type="text" name="phone_search" id="phone" value = '<%=phoneSearch%>' placeholder="Телефон"  />
	<input type="submit" id="search_submit" value="Шукати" />
	<br />
	<input type="button" value="Очистити" onclick="clearForm()"/>
	<a href="mainsearch.jsp" ><button type="button">Пааачистить</button></a>
	<input type="hidden" name="orderby" id="orderby" value='<%=orderBy%>' />
	
</form>
</div>
<div class="result">
<%-- <p>Record's retrieved: <c:out value="${rs.rowCount}" />, user: <c:out value="${login}" />  </p>
<p>Record's retrieved: ${rs.rowCount} </p> --%>
<table class="result-table" id="result" tabindex="0">
<%-- <col align="left" width="40%">
<col align="left" width="20%">
<col align="left" width="30%">
<col align="center" width="40">
<col align="center" width="40">
<col align="right" width="80">
 --%>
 <tr>
	<th class="th_fio"><a <%=orderBy.equals("OwnerName")?"class=\"order-selected\"":"" %>href="javascript: orderby('OwnerName')" title="сортувати за прізвищем">Прізвище</a></th>
	<th class="th_city"><a <%=orderBy.equals("CityName")?"class=\"order-selected\"":"" %>href="javascript: orderby('CityName')">Місто</a></th> 
	<th class="th_street"><a <%=orderBy.equals("StreetName")?"class=\"order-selected\"":"" %>href="javascript: orderby('StreetName')">Вулиця</a></th> 
	<th class="th_building"><a <%=orderBy.equals("cast(Building as decimal)")?"class=\"order-selected\"":"" %>href="javascript: orderby('cast(Building as decimal)')">Буд</a></th> 
	<th class="th_app"><a <%=orderBy.equals("cast(App as decimal)")?"class=\"order-selected\"":"" %>href="javascript: orderby('cast(App as decimal)')">Кв</a></th> 
	<th class="th_phone"><a <%=orderBy.equals("cast(PhoneNumber as decimal)")?"class=\"order-selected\"":"" %>href="javascript: orderby('cast(PhoneNumber as decimal)')">Телефон</a></th> 
</tr>
<c:forEach var="row" items="${rs.rows}">
	<tr>
    <td class="td_fio">${row.OwnerName}</td>
    <td class="td_city">${row.CityName}</td>
    <td class="td_street">${row.StreetName}</td>
    <td class="td_building">${row.Building}</td>
    <td class="td_app">${row.App}</td>
    <td class="td_phone">${row.PhoneNumber}</td>
    </tr>
</c:forEach>
</table>

</div>
<p id="blablabla" />
</body>


</html>